package com.suxin.config;

import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;


import java.util.HashMap;
import java.util.Map;

/**
 * @author Tang
 * @classname MyHandlerMethodReturnValueHandler
 * @description [  ]
 * @date 2021/3/29 22:59
 */
public class MyHandlerMethodReturnValueHandler implements HandlerMethodReturnValueHandler {

    private HandlerMethodReturnValueHandler methodReturnValueHandler;

    public MyHandlerMethodReturnValueHandler(HandlerMethodReturnValueHandler methodReturnValueHandler) {
        this.methodReturnValueHandler = methodReturnValueHandler;
    }


    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return methodReturnValueHandler.supportsReturnType(returnType);
    }

    @Override
    public void handleReturnValue(@Nullable Object returnValue, @Nullable MethodParameter returnType,
                                  @Nullable ModelAndViewContainer mavContainer, @Nullable NativeWebRequest webRequest) throws Exception {
        System.out.println("自定义的解析器执行了....");
        Map<String, Object> map = new HashMap<>(8);
        map.put("code", 200);
        map.put("data", returnValue);
        methodReturnValueHandler.handleReturnValue(map, returnType, mavContainer, webRequest);
    }
}